﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI;
using System.Data;
using ASPNETVN.PORTAL.Modules.NEWS.Components;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Modules.NEWS
{
    public enum ModulePages
    {
        Item,
        Detail,
        CategoryManager,
        FlowManager,
        ItemManager
    }

    public partial class Main : PortalModuleControl
    {
        private void loadChildControl()
        {
            const string basePath = "~/Modules/NEWS/";
            const string pageQuery = "page";
            string pageName = Request.QueryString[pageQuery];

            ModulePages page;
            if (pageName == null)
            {
                page = ModulePages.Item;
            }
            else
            {
                page = (ModulePages)Enum.Parse(typeof(ModulePages), Request.QueryString[pageQuery], true);
            }

            string src = string.Format("{0}Controls/{1}.ascx", basePath, page);
            ModulePage ctl = (ModulePage)this.LoadControl(src);
            ctl.ParentControl = this;
            ContentPane.Controls.Add(ctl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.loadChildControl();
            this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "page=AddEditItem");
            
            // Left Pane.
            LiteralControl literal = new LiteralControl("");

            literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
            // OptionView = None
            string url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "optionview=None"));
            literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Mới cập nhật</a></td></tr>", url);
            // OptionView = TopView
            url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "optionview=TopView"));
            literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Xem nhiều nhất</a></td></tr>", url);
            // OptionView = TopComment
            url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "optionview=TopComment"));
            literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Bình luận nhiều nhất</a></td></tr>", url);

            literal.Text += "</table>";

            // Manager Permission.
            if (PortalSecurity.HasManagerAuthorization(this.ModuleID))
            {
                literal.Text += "<table border='0' cellpadding='0' cellspacing='0' style='width: 100%'><tr><td class='VerticalMenuGroup' width='100%'>Management</td></tr></table>";
                literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.ItemManager);
                url = url.Substring(2);
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Bài viết</a></td></tr>", url);

                url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.CategoryManager);
                url = url.Substring(2);
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Danh mục</a></td></tr>", url);

                url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.FlowManager);
                url = url.Substring(2);
                literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» Luồng</a></td></tr>", url);
                literal.Text += "</table>";
            }

            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            foreach (DataRow row in rootRows)
            {
                literal.Text += "<table border='0' cellpadding='0' cellspacing='0' style='width: 100%'><tr><td class='VerticalMenuGroup' width='100%'>" + row["Name"] + "</td></tr></table>";
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    literal.Text += "<table border='0' cellpadding='4' cellspacing='0' style='width: 100%'>";
                    foreach (DataRow childRow in childRows)
                    {
                        url = HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}&categoryname={1}", childRow["ID"], Server.UrlEncode(childRow["Name"].ToString()));
                        url = url.Substring(2);
                        literal.Text += string.Format("<tr><td width='100%'><a href='{0}' class='VerticalMenuItem'>» {1}</a></td></tr>", url, childRow["Name"]);
                    }
                    literal.Text += "</table>";
                }
            }

            LeftPane.Controls.Add(literal);

        }
    }
}